var express = require('express');
var router = express.Router();
var Project = require('./../models/Project.js');
var User = require('./../models/User.js');
var extend = require('extend');
var config = require('./../config.json');

router.get('/appliers', function(req, res, next) {
    var user = req.user;
    user.getAppliers().then(function(applierList) {
        res.send(applierList);
    });
});

router.post('/handleApplier', function(req, res, next) {
    var user = req.user;
    var applier = req.body.postData.applier;
    user.handleApplier(applier).then(function(result) {
        res.send(result);
    });
});

router.post('/UpdateInfo', function(req, res, next) {
    var user = req.user;
    var data = req.body.postData;
    user.updateInfo(data).then(function(result) {
        //更新redis缓存
        if (result.toString() === "true") {
            user = extend(user, data);
            var nameSpace = config.redisNamespace;
            var authorUrl = config.authorUrl;
            var redis = require('redis');
            var redisClient = redis.createClient();
            redisClient.set(nameSpace + ':user:' + req.cookies.uid, JSON.stringify(user));
            redisClient.quit();
        }
        res.send(result);
    });
});


router.post('/changePassword', function(req, res, next) {
    var user = req.user;
    var postData = req.body.postData;
    user.changePassword(postData).then(function(result) {
        res.send(result);
    });
});


router.get('/authorizedUsers', function(req, res, next) {
    var user = req.user;
    user.getAuthorizedUsers().then(function(list) {
        res.send(list);
    });
});

router.post('/AuthorizedProjectList', function(req, res, next) {
    var user = extend(new User(), req.body.user);
    //重新切换回authentication
    user.authentication = user.authentication.code;
    if (user.hasAdminPower()) {
        res.status(403).send(false);
    }
    user.updateAuthorizedProjectList().then(function(result) {
        res.send(result);
    });
});

router.post('/freezeUser', function(req, res, next) {
    var targetUser = extend(new User(), req.body.user);
    //重新切换回authentication
    targetUser.authentication = targetUser.authentication.code;
    if (!req.user.hasAdminPower()) {
        res.status(401).send(false);
    }

    req.user.freezeUser(targetUser).then(function(result) {
        res.send(result);
    });
});

router.post("/UpdateUserAuthorizedProjectList", function(req, res, next) {
    if (!req.user.hasAdminPower()) {
        res.status(401).send(false);
    }
    req.user.updateUserAuthorizedProjectList(req.body.postData).then(function(result) {
        res.send(result);
    });
});

router.post('/ChangeUserAuthentication', function(req, res, next) {
    if (!req.user.hasReviewPower()) {
        res.status(401).send(false);
    }
    req.user.changeUserAuthentication(req.body.postData).then(function(result) {
        res.send(result);
    });
});

//获取用户可以审核的项目
router.get('/ReviewProjectList', function(req, res, next) {
    if (!req.user.hasReviewPower())
        res.status(401).send(false);
    else {
        req.user.getReviewProjectList().then(function(data) {
            res.send(data);
        });
    }
});

module.exports = router;
